const app = getApp()
var answer = ''//选的答案
var correct_answer=''//正确答案
var question_number=''//题号
var id=''//用户id
var code_correct = 'blue'//选对的颜色
var code_err = "red"//选错的颜色
var score=''//总成绩
var bg=''//背景颜色
var co=''//字体颜色
Page({
  data: {
    films: [],
    hasMore: true,
    showLoading: true,
    answer_option:true,//正确答案的显示
    start: 0,
    list:{},
    openId:'',
    next_question:true,//下一题按钮
    nan:0
  },
  onLoad: function () {
    var that = this;
    wx.request({
      // 题库API
      url: 'http://10.20.58.235:8080/v1/open/question/manager',
      header: {
        'Content-Type': 'application/xml',
      },
      success: function (res) {
        //console.log("=============", res.data.options["0"].content);
        //裁剪字符串
        for(var index in res.data.options){
          res.data.options[index].content = res.data.options[index].content.substring(1, );
        }
        //console.log(res.data)
        answer=res;
        //正确答案
        correct_answer=res.data.answer,
        //试题id
        question_number =res.data.id,
        that.setData({
          list: res.data,
          section: false,
        })
        //console.log(res.data.options.length)
      },
    }),
    wx.getStorage({
      key: 'userid',
      success: function (res) {
        that.setData({
          userid: res.data
        })
        //赋值
        id = that.data.userid
        //console.log('========='+id)
      },
    })

      //获取用户信息
    wx.getUserInfo({
      key: 'userid',
      success: function (res) {
        wx.request({
          url: 'http://10.20.58.235:8080/v1/open/information/manager/summary/%7Bid%7D?id=' + id,
          method: 'GET',
          header: {
            'Content-Type': 'application/xml',
          },
          success: function (res) {
            //console.log(res);//后天返回关于成绩的数据
            score=res.data.score;
            that.setData({
              score: res.data,
              section: false
            })
            //console.log(id)
          },
        })
      },
    })
  },
  chart: function (res) {
    var that = this;
    //console.log(res)
    var answer = res.currentTarget.dataset.param;
    //console.log("正确答案" +  correct_answer);
    //console.log("点击的是"+  answer)
    var nan = res.currentTarget.dataset.index+1;//获取点击的是第几个
    that.setData({
      nan: nan
    })
    //console.log(nan)
    if (correct_answer === answer){
        //console.log("正确")
        that.setData({
          bg: 'blue',
          co: 'white'
        })
      } else {
        console.log('错误');
        that.setData({
          bg: 'red',
          co: 'white'
        })
      }
    wx.request({
      url: 'http://10.20.58.235:8080/v1/open/operation/manager/submit?userid=' + id + '&queid=' + question_number + '&answer=' + answer,
      method:'PUT',
      success:function(e){
        //console.log("--------------", e.data)//后台返回数据
        if (e.data!=false){
          that.setData({
            answer_option: true,//正确答案
            next_question:false//下一题按钮
          })
        }else{
          that.setData({
            answer_option:false,
            next_question: false//下一题按钮
          })
          //定时器
          //setTimeout(that.next_question(), 3000);
        }
      },
    })
  },
  // 点击获取下一题
  next_question:function(){
    var that=that;
    wx.redirectTo({
      url:'problem',
    })
  },
  end:function(res){
    var that=this;
    wx.showModal({
      content: '是否结束开始？',
      success(res) {
        if (res.confirm) {
          wx.switchTab({
            url: '../../pages/my/my',
          })
        } else if (res.cancel) {
          
        }
      }
    })
  }
})